package com.czwx.bee.business.manager.modular.sys.controller;

import com.czwx.bee.base.common.dto.ResponseDto;
import com.czwx.bee.base.common.exception.BizExceptionEnum;
import com.czwx.bee.base.common.interfaces.BpUserStatusEnum;
import com.czwx.bee.base.common.interfaces.BpUserTypeEnum;
import com.czwx.bee.business.manager.exception.ManagerExceptionEnum;
import com.czwx.bee.business.manager.modular.sys.ShopAddUserDto;
import com.czwx.bee.business.manager.modular.sys.model.SecurityUser;
import com.czwx.bee.business.manager.modular.sys.model.User;
import com.czwx.bee.business.manager.modular.sys.service.UserService;
import com.czwx.bee.business.manager.utils.ManagerExceptionUtil;
import com.czwx.bee.business.manager.utils.SecurityUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;

/**
 * @Author:wiki
 * @Date:Create in 2018/7/16 0016 下午 4:27
 */

@RestController
@RequestMapping("/shop_user")
@Api(value = "ShopController",tags = "商户相关")
public class ShopController {


    @Autowired
    private UserService userService;

    @PostMapping("/add_shop_user")
    @ApiOperation(value = "开通商户普通账号", notes = "开通商户普通账号", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "请输入凭证", required = true, dataType = "String", paramType = "header"),
            @ApiImplicitParam(name = "user", value = "请输入用户信息", required = true, dataType = "User", paramType = "body")
    })
    public ResponseDto addUser(@Validated @RequestBody User user){
        ResponseDto rs = new ResponseDto(BizExceptionEnum.SUCCESSFUL_OPERATION);
        SecurityUser securityUser=SecurityUtil.getSecurityUser();
        Long drainageId=securityUser.getDrainageId();

        if(drainageId==null){
            rs.ChangeBizException(BizExceptionEnum.FAIL_OPERATION);
            return rs;
        }
        User checkUser=new User();
        checkUser.setLoginName(user.getLoginName());
        checkUser=userService.getBySelf(checkUser);
        if(checkUser!=null){
            return ManagerExceptionUtil.tranResponseDto(rs,ManagerExceptionEnum.HAS_REGIST);
        }
        user.setStatus(BpUserStatusEnum.Enable.getCode());
        user.setUserType(BpUserTypeEnum.SHOP_USER.getCode());
        userService.insert(user);
        rs.setData(user);
        return rs;
    }

    @PostMapping("/add_shop_admin")
    @ApiOperation(value = "开通商户管理员账号", notes = "开通商户管理员账号", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "user", value = "请输入用户信息", required = true, dataType = "ShopAddUserDto", paramType = "body")
    })
    public ResponseDto addAdmin(@Validated @RequestBody ShopAddUserDto shopAddUserDto){
        ResponseDto rs = new ResponseDto(BizExceptionEnum.SUCCESSFUL_OPERATION);
        User checkUser=new User();
        checkUser.setLoginName(shopAddUserDto.getLoginName());
        checkUser=userService.getBySelf(checkUser);
        if(checkUser!=null){
            return ManagerExceptionUtil.tranResponseDto(rs,ManagerExceptionEnum.HAS_REGIST);
        }
        User user=new User();
        BeanUtils.copyProperties(shopAddUserDto,user);
        user.setStatus(BpUserStatusEnum.Enable.getCode());
        user.setUserType(BpUserTypeEnum.SHOP_ADMIN.getCode());
        userService.insert(user);
        rs.setData(user);
        return rs;
    }

    @ApiOperation(value = "修改商户员工", notes = "修改员工model", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "请输入凭证", required = true, dataType = "String", paramType = "header"),
            @ApiImplicitParam(name = "user", value = "请输入用户信息", required = true, dataType = "User", paramType = "body")
    })
    @PostMapping("/update")
    public ResponseDto update(@RequestBody User user){
        ResponseDto rs=new ResponseDto(BizExceptionEnum.SUCCESSFUL_OPERATION);
        userService.update(user);
        return rs;
    }

    @PostMapping("disable_shop")
    @ApiOperation(value = "禁用商户所有账号", notes = "该渠道所有号停用", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "请输入凭证", required = true, dataType = "String", paramType = "header"),
            @ApiImplicitParam(name = "channelId", value = "渠道id", required = true, dataType = "Long", paramType = "query"),
    })
    public ResponseDto DisableShop(@NotNull(message = "渠道id不能为空") Long channelId){
        ResponseDto rs=new ResponseDto(BizExceptionEnum.SUCCESSFUL_OPERATION);
        userService.disableDrainage(channelId);
        return rs;
    }

    @PostMapping("disable_shopUser")
    @ApiOperation(value = "禁用商户下员工号", notes = "商户管理员操作", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "请输入凭证", required = true, dataType = "String", paramType = "header"),
            @ApiImplicitParam(name = "channelId", value = "渠道id", required = true, dataType = "Long", paramType = "query"),
    })
    public ResponseDto DisableShopUser(@NotNull(message = "渠道id不能为空") Long userId){
        ResponseDto rs=new ResponseDto(BizExceptionEnum.SUCCESSFUL_OPERATION);
        SecurityUser securityUser=SecurityUtil.getSecurityUser();
        User user=userService.getById(userId);
        if(user.getDrainageId()!=securityUser.getDrainageId()){
            return  ManagerExceptionUtil.tranResponseDto(rs,ManagerExceptionEnum.NOT_FIX);
        }
        User updateUser=new User();
        updateUser.setId(userId);
        updateUser.setStatus(BpUserStatusEnum.Discontinuation.getCode());
        userService.update(updateUser);
        return rs;
    }
}
